import 'package:check_app/dao/version_dao.dart';
import 'package:check_app/scoped_model/app_model.dart';
import 'package:check_app/util/pref_util.dart';
import 'package:check_app/util/uploadApp_util.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:modal_progress_hud/modal_progress_hud.dart';
import 'package:scoped_model/scoped_model.dart';

class Version extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => new _VersionState();
}

class _VersionState extends State<Version> {
  bool _loading = false;
  int id;
  String version;
  String remark;
  String downloadUrl;

  @override
  void initState() {
    super.initState();
    getData();
  }

  Future<Null> getData() async {
    setState(() {
      _loading = true;
    });
    try {
      var result = await VersionDao.fetch();
      setState(() {
        _loading = false;
      });
      if (result.success) {
        setState(() {
          id = result.data.id;
          version = result.data.version;
          remark = result.data.remark;
          downloadUrl = result.data.downloadUrl;
        });
      } else {
        Fluttertoast.showToast(
          msg: result.message,
        );
      }
    } catch (e) {
      print(e);
      setState(() {
        _loading = false;
      });
    }
    return null;
  }

  uploadApp() async {
    final currentVersion = ScopedModel.of<GlobalModel>(context).currentVersion;
    if (currentVersion == version) {
      Fluttertoast.showToast(
        msg: '当前已是最新版本',
      );
    } else {
      final result =
          await PrefUtil().openAlertDialog(context, '检测到新版本，是否立即更新？');
      if (result == 'ok') {
        print('更新');
        CheckUpdate().upload();
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color.fromRGBO(248, 248, 249, 1),
      appBar: AppBar(
        centerTitle: true,
        title: Text('系统版本'),
      ),
      body: SafeArea(
        child: ModalProgressHUD(
          inAsyncCall: _loading,
          child: Column(
            children: <Widget>[
              // 版本信息
              Container(
                width: double.infinity,
                padding: EdgeInsets.fromLTRB(16, 60, 16, 32),
                child: Column(
                  children: <Widget>[
                    CircleAvatar(
                      radius: 28.0,
                      backgroundImage: AssetImage('assets/images/touxiang.png'),
                    ),
                    Container(
                      margin: EdgeInsets.only(top: 12),
                      child: Column(
                        children: <Widget>[
                          Container(
                            alignment: Alignment.center,
                            child: Text(
                              '优策云检',
                              style: TextStyle(
                                fontSize: 18,
                                height: 25 / 18,
                              ),
                            ),
                          ),
                          Container(
                            margin: EdgeInsets.only(top: 6),
                            alignment: Alignment.center,
                            child: ScopedModelDescendant<GlobalModel>(
                              rebuildOnChange: true, //改变的时候是否重建
                              builder: (context, _, model) => Text(
                                '当前版本：' + model.currentVersion,
                                style: TextStyle(
                                  fontSize: 12,
                                  height: 15 / 12,
                                  color: Color.fromRGBO(150, 151, 154, 1),
                                ),
                              ),
                            ),
                          ),
                          Container(
                            margin: EdgeInsets.only(top: 6),
                            alignment: Alignment.center,
                            child: Text(
                              version != null ? '最新版本：' + version : '最新版本：',
                              style: TextStyle(
                                fontSize: 12,
                                height: 15 / 12,
                                color: Color.fromRGBO(240, 64, 64, 1),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    Container(
                      width: 8,
                      height: 13,
                      child: Image(
                        image: AssetImage('assets/images/chevron.png'),
                        width: 8,
                        height: 13,
                      ),
                    ),
                  ],
                ),
              ),

              // 更新版本
              GestureDetector(
                onTap: () {
                  uploadApp();
                },
                child: Container(
                  width: double.infinity,
                  height: 46,
                  margin: EdgeInsets.only(top: 12),
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    color: Color.fromRGBO(255, 255, 255, 1),
                    border: Border(
                      top: BorderSide(
                        width: 0.5,
                        color: Color.fromRGBO(229, 229, 229, 1),
                      ),
                      bottom: BorderSide(
                        width: 0.5,
                        color: Color.fromRGBO(229, 229, 229, 1),
                      ),
                    ),
                  ),
                  child: Text(
                    '更新版本',
                    style: TextStyle(
                      fontSize: 16,
                      height: 22 / 16,
                      color: Color.fromRGBO(240, 64, 64, 1),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
